<template>
  <div class="app-container">
    <div class="crud-group">
      <el-input
        v-model="paramsForm.mark"
        type="text"
        placeholder="任务日期"
        clearable
      />
      <el-select
        v-model="paramsForm.flag"
        placeholder="标识"
        clearable
      >
        <el-option
          v-for="item of ['WG','JQ']"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
     
      <el-input
        v-model="paramsForm.dbName"
        type="text"
        placeholder="库名"
        clearable
      />
      <el-input
        v-model="paramsForm.tbName"
        type="text"
        placeholder="表名"
        clearable
      />
      <el-input
        v-model="paramsForm.dbTbName"
        type="text"
        placeholder="库表名"
        clearable
      />
      <el-button
        type="success"
        icon="el-icon-search"
        @click="getData"
      >
        搜索
      </el-button>
      <el-button
        type="warning"
        icon="el-icon-refresh-left"
        @click="resetForm"
      >
        重置
      </el-button>
      
    </div>

    <el-table
      ref="table"
      v-loading="isLoading"
      :data="data"
      border
      @selection-change="handleSelectionChange"
      @select-all="()=>{
        if(selection){
          this.$refs.table.clearSelection()
        }
      }"
    >
      <el-table-column
        type="selection"
        width="55"
      />
      <el-table-column
        label="任务日期"
        prop="mark"
        sortable
        fixed
        min-width="20"
      />
      <el-table-column
        label="标识"
        prop="flag"
        sortable
        fixed
        min-width="20"
      />
      <el-table-column
        label="库名"
        prop="dbName"
        sortable
        min-width="20"
      />
      <el-table-column
        label="表名"
        prop="tbName"
        sortable
        min-width="30"
      />
      <el-table-column
        label="库表名"
        prop="dbTbName"
        sortable
        min-width="30"
      />
      <el-table-column
        label="建表语句"
        prop="createTbPath"
        sortable
      />
     
      <!-- <el-table-column
        label="目标总数"
        prop="targetCountNumber"
        sortable
        show-overflow-tooltip
      /> -->
    </el-table>
    <div class="pagination-container">
      <el-pagination
        :current-page="paramsForm.current"
        :page-size="paramsForm.size"
        layout="prev, pager, next,sizes,total"
        :total="total"
        @size-change="(val)=>{
          paramsForm.current = 1
          paramsForm.size = val
          getData()
        }"
        @current-change="(val)=>{
          paramsForm.current = val
          getData(false)
        }"
      />
    </div>

    <TableNameList
      :dialog-visible="dialogVisible"
      :table-name-list="selectedName"
      @close="()=>{
        dialogVisible = false
      }"
    />
    <ReRunDialog
      :dialog-visible="reRunDialogVisible"
      :selection="selection"
      :type="type"
      @close="()=>{
        reRunDialogVisible = false
        getData()
      }"
    />
    <ContinueDialog
      :dialog-visible="continueDialogVisible"
      :selection="selection"
      :type="type"
      @close="()=>{
        continueDialogVisible = false
        getData()
      }"
    />
  </div>
</template>

<script>
import cbApi from '@/api/coldStandby/index'
import TableNameList from './components/TableNameListDialog.vue'
import ReRunDialog from './components/ReRunDialog.vue'
import ContinueDialog from './components/ContinueDialog.vue'

const form = {
  taskId: '',
  flag: '',
  mark: '',
  current: 1,
  size: 20,
  tbName: '',
  dbName: '',
  dbTbName: ''
}

export default {
  components: {TableNameList, ReRunDialog, ContinueDialog},
  data() {
    return {
      data: [],
      total: 0,
      isLoading: false,
      paramsForm: {...form},
      dialogVisible: false,
      fullAuditDialogVisible: false,
      statisticsDetailsDialogVisible: false,
      reRunDialogVisible: false,
      continueDialogVisible: false,
      selectedTaskId: '',
      selectedFlag: '',
      selectedName: '',
      selection: null,
      type: ''
    }
  },
  created() {
    this.getData()
  },

  methods: {
    getData(resetPage = true) {
      this.isLoading = true
      if (resetPage) {
        this.paramsForm.current = 1
      }
      cbApi.cbAllTable(this.paramsForm)
        .then((res) => {
          this.data = res.records
          this.total = res.total
        })
        .finally(() => {
          this.isLoading = false
        })
    },
    reRun() {
      if (this.type === '统计') {
        this.reRunDialogVisible = true
        return 
      } else {
        const form = {
          flag: this.selection.flag,
          order: this.selection.taskName.includes('【LT】') ? '' : 'QL',
          mark: this.selection.mark,
          dbName: this.selection.dbName
        }
        cbApi.reRunFullAudit(form)
        setTimeout(() => {
          this.$message.success('开始重跑')
          this.getData()
        }, 500)
      }
    },
    async pause() {
      this.type === '统计' ? await cbApi.pauseCbParamConfig() : await cbApi.pauseFullAudit()
      this.$message.success('暂停成功')
      this.getData()
    },
    continue() {
      if (this.type === '统计') {
        this.continueDialogVisible = true
        return 
      }
      const form = {
        flag: this.selection.flag,
        order: this.selection.taskName.includes('【LT】') ? '' : 'QL',
        mark: this.selection.mark,
        dbName: this.selection.dbName,
        tableName: this.selection.tableName
      }
      cbApi.continueFullAudit(form)
      setTimeout(() => {
        this.$message.success('继续重跑')
        this.getData()
      }, 500)
    },
    handleCommand(command) {
      if (command.includes('FA')) {
        this.type = '稽核'
        this[command.replace('FA', '')]()
        return
      } else {
        this.type = '统计'
      }
      this[command]()
    },
    resetForm() {
      this.paramsForm = {...form}
      this.getData()
    },
    handleSelectionChange(val) {
      if (val.length > 1) {
        this.$refs.table.clearSelection()
        this.$refs.table.toggleRowSelection(val[val.length - 1])
      }
      this.selection = val[val.length - 1]
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
